package cn.zebra.dev.admin.module.product.controller;

import cn.zebra.dev.admin.module.login.SecurityAdminService;
import cn.zebra.dev.admin.module.product.dto.ProductCategoryDTO;
import cn.zebra.dev.admin.module.product.vo.ProductCategoryVO;
import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.product.module.category.entity.CategoryEntity;
import cn.zebra.dev.product.module.category.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.LinkedList;
import java.util.List;

/**
 * @author runnable@sina.cn
 */
@RequestMapping("/product/category")
@RestController
@Api(tags = "分类管理")
public class ProductCategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SecurityAdminService adminService;

    @GetMapping("level_list")
    @ApiOperation("层级分类列表")
    public Result<List<ProductCategoryDTO>> firstLevelList(Integer levelType) {
        List<CategoryEntity> categoryEntity = categoryService.findByLevelType(levelType);
        List<ProductCategoryDTO> categoryDTOS = new LinkedList<>();
        if (categoryEntity.size() > 0){
            categoryEntity.forEach(categoryEntity1 -> {
                ProductCategoryDTO productCategoryDTO = new ProductCategoryDTO();
                BeanUtils.copyProperties(categoryEntity1,productCategoryDTO);
                categoryDTOS.add(productCategoryDTO);
            });
        }
        return Result.success(categoryDTOS);
    }

    @GetMapping("/get/{id}")
    @ApiOperation("获取分类信息")
    public Result<ProductCategoryDTO> get(@PathVariable Long id) {
        CategoryEntity entity = categoryService.findById(id);
        ProductCategoryDTO dto = new ProductCategoryDTO();
        BeanUtils.copyProperties(entity, dto);
        return Result.success(dto);
    }

    @PostMapping("/save")
    @ApiOperation("新增分类")
    public Result save(@RequestBody @Valid ProductCategoryVO vo) {
        CategoryEntity entity = new CategoryEntity();
        BeanUtils.copyProperties(vo, entity);
        entity.setCategoryType(10);
        if (vo.getLevelType() == 1){
            entity.setParentId(0L);
        }else if (vo.getLevelType() == 2){
            if (!comparative(1,vo.getSuperStratum())){
                return Result.error("没有此上级目录,针对于一级分类");
            }
            entity.setParentId(vo.getSuperStratum());
        }else if (vo.getLevelType() == 3){
            if (!comparative(2,vo.getSuperStratum())){
                return Result.error("没有此上级目录，针对于二级分类");
            }
            entity.setParentId(vo.getSuperStratum());
        }

        if (categoryService.saveCategory(entity, adminService.getId())) {
            return Result.success("添加成功!");
        }
        return Result.error("保存失败!");
    }


    @PostMapping("/update/{id}")
    @ApiOperation("修改分类")
    public Result save(@PathVariable Long id, @RequestBody @Valid ProductCategoryVO vo) {
        CategoryEntity entity = new CategoryEntity();
        BeanUtils.copyProperties(vo, entity);
        entity.setCategoryType(0);
        if (entity.getParentId() == null) {
            entity.setParentId(0L);
        }
        if (categoryService.updateCategory(id, entity, adminService.getId())) {
            return Result.success("修改成功!");
        }
        return Result.success("修改失败!");
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除分类")
    public Result delete(@PathVariable Long id) {
        CategoryEntity entity = new CategoryEntity();
        entity.setDeleteFlag(true);
        if (categoryService.update(entity, "id = {0}", id)) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败!");
    }

    //比较分类是否存在
    private boolean comparative(Integer levelType,Long parentId){
        List<CategoryEntity> categoryEntity = categoryService.findIdByLevel(levelType);
        List<Long> flag = new LinkedList<>();
        categoryEntity.forEach(categoryEntity1 -> {
            flag.add(categoryEntity1.getId());
        });
        return flag.contains(parentId);
    }
}
